﻿package {
	
	import flash.text.*;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class TextBox {
		
		private var textfield:TextField;
		private var format:TextFormat;
		private var editor:Editor;
		
		private var offset:Point;
		private var mDown:Boolean;
		
 		public function TextBox(e:Editor, X:int = 0, Y:int = 0, w:int = 0, h:int = 0, font:String = "Times New Roman", size:String = "12") {
			editor = e;
			textfield = new TextField();
			textfield.text = "Text";
			textfield.x = X;
			textfield.y = Y;
			textfield.width = w;
			textfield.height = h;
			format = new TextFormat();
			format.font = font;
			format.size = Number(size);
			textfield.multiline = true;
			textfield.border = true;
			textfield.type = TextFieldType.INPUT;
			textfield.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
			textfield.addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
			textfield.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
			textfield.setTextFormat(format);
			mDown = false;
		}
		
		public function getTextField():TextField { return textfield; }
		public function getFont():String { return format.font; }
		public function getFontSize():Number { return Number(format.size); }
		public function getColor():uint { return uint(format.color); }
		public function getBgColor():uint { return textfield.backgroundColor; }
		
		public function setPos(attr:String, val:Number) {
			switch(attr) {
				case "x":
					textfield.x = val;
					break;
				case "y":
					textfield.y = val;
					break;
				case "w":
					textfield.width = val;
					break;
				case "h":
					textfield.height = val;
					break;
			}
		}
		
		public function setFont(f:String) {
			format.font = f;
			textfield.setTextFormat(format);
		}
		
		public function setSize(s:Number) {
			format.size = s;
			textfield.setTextFormat(format);
		}
		
		public function setColor(color:uint) {
			format.color = color;
			textfield.setTextFormat(format);
		}
		
		public function setBgColor(color:uint, isClear:Boolean = false) {
			if(!isClear) textfield.backgroundColor = color;
			textfield.background = !isClear;
			trace(textfield.background + " " + textfield.backgroundColor + " " + color);
		}
		
		public function fromXML(xml:String) {
			var pieces:Array = xml.match(">.+</Textbox>");
			textfield.text = String(pieces[0]).slice(1,-10);
			
			pieces = xml.match("x=\".+\"");
			var attr = String(pieces[0]).split('"');
			//trace(attr);
			textfield.x = attr[3]; textfield.y = attr[5];
			textfield.width = attr[7]; textfield.height = attr[9];
			format.font = attr[11]; format.size = attr[13];
			format.color = parseInt("0x" + attr[15], 16); 
			if (attr[17] == "clear") {
				textfield.background = false;
				textfield.backgroundColor = 0xFFFFFF;
			} else {
				textfield.background = true;
				textfield.backgroundColor = parseInt("0x" + attr[17],16);
			}
			textfield.setTextFormat(format);
			
		}
		
		public function toString(i:int):String {
			var xml:String = "<Textbox\n";
				xml += "\tindex=\"" + i + "\"\n";
				xml += "\tx=\"" + textfield.x + "\"\n";
				xml += "\ty=\"" + textfield.y + "\"\n";
				xml += "\twidth=\"" + textfield.width + "\"\n";
				xml += "\theight=\"" + textfield.height + "\"\n";
				xml += "\tfont=\"" + format.font + "\"\n";
				xml += "\tsize=\"" + format.size + "\"\n";
				xml += "\ttextcolor=\"" + (format.color == null ? addZeros(0x000000):addZeros(format.color.toString(16))) + "\"\n";
				xml += "\tbgcolor=\"" + (textfield.background ? addZeros(textfield.backgroundColor):"clear") + "\"\n";
				xml += ">\n";
				xml += textfield.text;
				xml += "\n</Textbox>\n";
			return xml;
		}
		
		private function showProperties() {
			
		}
		
		private function mouseDownListener(e:MouseEvent):void {
			offset = new Point(e.localX, e.localY);
			mDown = true;
		}
		
		private function mouseUpListener(e:MouseEvent):void {
			mDown = false;
			//trace(editor.cursor);
			if (editor.cursor != "eraser") editor.showProperties("textbox", this);
			else {
				var index:int = editor.elements.indexOf(this);
				editor.elements.splice(index, 1);
				editor.elementTypes.splice(index, 1);				
				if (editor.contains(textfield)) editor.removeChild(textfield);
			}
			
		}
		
		public function mUp():void { mDown = false; }
		
		public function moveBox(e:MouseEvent) {			
			textfield.x = e.stageX - offset.x;
			textfield.y = e.stageY - offset.y;
			if (textfield.x < 100) textfield.x = 100;
			if (textfield.y < 100) textfield.y = 100;
			if (textfield.x > 1024 - textfield.width) textfield.x = 1024 - textfield.width
			if (textfield.y > 764 - textfield.height) textfield.y = 768 - textfield.height
		}
		
		private function mouseMoveListener(e:MouseEvent) {
			if(mDown) {
				moveBox(e);
			}
		}
		
		private function addZeros(val:uint):String {
			var str:String = "";
			if (val < 100000) str += "0";
			if (val < 10000) str += "0";
			if (val < 1000) str += "0";
			if (val < 1000) str += "0";
			if (val < 10) str += "0";
			str += val.toString(16);
			trace(str);
			return str;
		}
		
	}
}